package com.ellricky.baselib.ui.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

import androidx.annotation.Nullable;

import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.util.FileUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.util.photo.TakePhotoUtil;

import org.devio.takephoto.app.TakePhoto;
import org.devio.takephoto.app.TakePhotoImpl;
import org.devio.takephoto.model.InvokeParam;
import org.devio.takephoto.model.TContextWrap;
import org.devio.takephoto.model.TResult;
import org.devio.takephoto.permission.InvokeListener;
import org.devio.takephoto.permission.PermissionManager;
import org.devio.takephoto.permission.PermissionManager.TPermissionType;
import org.devio.takephoto.permission.TakePhotoInvocationHandler;
import org.jetbrains.annotations.NotNull;

import java.io.File;

/**
 * - 支持通过相机拍照获取图片
 * - 支持从相册选择图片
 * - 支持从文件选择图片
 * - 支持多图选择
 * - 支持批量图片裁切
 * - 支持批量图片压缩
 * - 支持对图片进行压缩
 * - 支持对图片进行裁剪
 * - 支持对裁剪及压缩参数自定义
 * - 提供自带裁剪工具(可选)
 * - 支持智能选取及裁剪异常处理
 * - 支持因拍照Activity被回收后的自动恢复
 * Author: crazycodeboy
 * Date: 2016/9/21 0007 20:10
 * Version:4.0.0
 * 技术博文：http://www.devio.org
 * GitHub:https://github.com/crazycodeboy
 * Email:crazycodeboy@gmail.com
 */
public abstract class BaseTakePhotoActivity extends BaseToolBarActivity
        implements TakePhoto.TakeResultListener, InvokeListener, OnTakePhotoListener {

    private InvokeParam invokeParam;
    protected TakePhoto takePhoto;

    protected Uri imageUri;

  /*  @Override
    public void takeSuccess(TResult result) {
//        Glide.with(this)
//                .load(result.getImage().getCompressPath())
//                .apply(new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE))
//                .into(viewById1);



    }*/

    @Override
    public void takeFail(TResult result, String msg) {
        ToastUtil.show(this, "操作失败");
    }

    @Override
    public void takeCancel() {
        ToastUtil.show(this, "操作取消");
    }

    @Override
    public TPermissionType invoke(InvokeParam invokeParam) {
        TPermissionType type = PermissionManager.checkPermission(
                TContextWrap.of(this), invokeParam.getMethod()
        );
        if (TPermissionType.WAIT.equals(type)) {
            this.invokeParam = invokeParam;
        }
        return type;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        getTakePhoto().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
    }


    @Override
    public void initUIView() {
        super.initUIView();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        getTakePhoto().onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        getTakePhoto().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }


    public TakePhoto getTakePhoto() {
        if (takePhoto == null) {
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
        }
        return takePhoto;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NotNull String[] permissions, @NotNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.TPermissionType type = PermissionManager.
                onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.
                handlePermissionsResult(this, type, invokeParam, this);
    }


    @Override
    public void onClickItem(DialogInterface dialogInterface, int item) {
        dialogInterface.dismiss();

        createImageUri();
        TakePhotoUtil.configTakePhotoOption(takePhoto);
        TakePhotoUtil.configCompress(takePhoto);

        if (item == 0) {
            // 拍照
            TakePhotoUtil.takeCamera(takePhoto, null, imageUri);
        } else if (item == 1) {
            TakePhotoUtil.takePicOne(takePhoto, null, imageUri);
        }
    }

    protected void createImageUri() {
        File file = FileUtil.getFileDirE(this, Environment.DIRECTORY_PICTURES);
        String fileName = System.currentTimeMillis() + ".jpg";

        file = new File(file, fileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        imageUri = Uri.fromFile(file);

    }
}
